<?php

namespace common\models\business;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "business_vaccine_set".
 *
 * @property string $id
 * @property string $batch
 * @property string $vaccinename
 * @property integer $date
 * @property integer $createtime
 */
class BusinessVaccineSet extends \yii\db\ActiveRecord
{



    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '疫苗设置',      //用于修改了什么的显示
        'mainfield' => 'id',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => ['createtime','date'],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];



    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_vaccine_set';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['batch','vaccinename','date','price'],'required'],
            [['batch'], 'string', 'max' => 30],
            [['vaccinename'], 'string', 'max' => 50],
            [['remark'], 'string', 'max' => 500],
            [['createtime','date'],'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'batch' => '仔猪的批次',
            'vaccinename' => '疫苗名称',
            'date' => '应该疫苗时间',
            'createtime' => '创建时间',
            'remark' => '疫苗备注',
            'price' => '疫苗单价'
        ];
    }


    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->createtime = time();
        }
        $this->date = strtotime($this->date);
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }



    public function getBatch()
    {
        $batchs = (new Query())->select('distinct(batch) as batch')->from(BusinessPig::tableName())->all();
        foreach ($batchs as $key => $row){
            if(empty($row['batch'])){
                continue;
            }
            $batch[$row['batch']] = $row['batch'];
        }
        unset($batchs);
        return $batch;
    }


}
